﻿using PwjCy.Lite.EntityLite;
using PwjCy.Lite.EntityLite.Imp;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace PwjCy.Lite.Storage.Imp
{

    public abstract class AbstractPreparedStatement : AbstractStatement, IPreparedStatement, IStatement
    {
        protected bool _isPrepared;
        protected AbstractPreparedStatement(IDbCommand cmd)
            : base(cmd)
        {

        }
        public virtual void ClearParameter()
        {
            base._command.Parameters.Clear();
        }

        public virtual object GetParameterValue(string key)
        {
            return base._command.Parameters[key];
        }

        public abstract void SetParameter(string key, object val);

        public abstract void SetParameter(string key, object val, DbType type, int len);

        public abstract void SetParameter(string key, object val, DbType type, int len, ParameterDirection direction);
        public virtual void SetParameter(IPwjCyObject pwjcyObject)
        {
            EntityLiteRelationMap.Instance.SetParamValue(this, pwjcyObject);
        }
        public void SetParameterValue(int index, object val)
        {
            ((IDataParameter)base._command.Parameters[index]).Value = val;
        }

        //不懂干嘛的
        protected virtual void Prepared() { bool flag1 = this._isPrepared; }
        protected override IStatement CreateInstance(IConnection conn, string sql)
        {
            return conn.CreateStatement(sql);
        }
        public override DataSet ExecuteDataSet()
        {
            this.Prepared();
            try
            {
                this.InitializeAdapter();
                DataSet ds = new DataSet();
                base._adapter.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public override void ExecuteDataSet(DataSet ds)
        {
            this.Prepared();
            this.InitializeAdapter();
            base._adapter.Fill(ds);
        }
        public override int ExecuteNonQuery()
        {
            this.Prepared();
            return base.ExecuteNonQuery();
        }
        public override IResultReader ExecuteReader()
        {
            this.Prepared();
            return base.ExecuteReader();
        }
        public override IDataReader ExecuteReaderDirectly()
        {
            this.Prepared();
            return base.ExecuteReaderDirectly();
        }
        public override object ExecuteScalar()
        {
            this.Prepared();
            return base.ExecuteScalar();
        }
    }
}
